This page last changed on Sep 08, 2006 by bowens.

LESSON 5: Use the WFS Capabilities of GeoServer

Web Feture Service (WFS) provides the tools needed to query, edit, insert, and delete features. We are going to cover some basic WFS requests and get you comfortable with what to expect.

Get Feature Request

The easiest request is the get feature request; easiest, well safest, because you can't blow away your data! That's why we are going to start with it.

<wfs:GetFeature service="WFS" version="1.0.0"
  outputFormat="GML2"
  xmlns:topp="http://www.openplans.org/topp"
  xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wfs
                      http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
  <wfs:Query typeName="topp:states">
    <ogc:Filter>
       <ogc:FeatureId fid="states.3"/>
    </ogc:Filter>
    </wfs:Query>
</wfs:GetFeature>

This request specifies a query on the topp:states layer. It then uses a filter to select all features with a featureId of states.3.
What this will do is go through all features in the states dataset and return the ones with the ID of states.3; hopefully only one feature.

Lets try this request out.

  1. Head over to the demo request page of geoserver (http://localhost:8080/geoserver/demoRequest.do)
  2. In the url field enter: http://localhost:8080/geoserver/wfs
  3. In the body field enter the above xml request.
  4. Hit Submit.

You should get back some scary GML that is the feature with the ID of states.3.

Get Feature in BBOX

This is a very common request; it will select all features within a bounding box. Lets take a look at it:

<wfs:GetFeature service="WFS" version="1.0.0"
  outputFormat="GML2"
  xmlns:topp="http://www.openplans.org/topp"
  xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wfs
                      http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
  <wfs:Query typeName="topp:states">
    <wfs:PropertyName>topp:STATE_NAME</wfs:PropertyName>
    <wfs:PropertyName>topp:PERSONS</wfs:PropertyName>
    <ogc:Filter>
      <ogc:BBOX>
        <ogc:PropertyName>the_geom</ogc:PropertyName>
        <gml:Box srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
           <gml:coordinates>-75.102613,40.212597 -72.361859,41.512517</gml:coordinates>
        </gml:Box>
      </ogc:BBOX>
   </ogc:Filter>
  </wfs:Query>
</wfs:GetFeature>

As you can see there are 4 parts to it:

  • The query tag. It specifies to run the query against the topp:states layer
  • The PropertyName topp:STATE_NAME. This says that we want this attribute returned back to us
  • The PropertyName top:PERSONS. This says that we also want this attribute returned back to us
  • The bounding box that says "we want all features in this bounding box"

So, if you make this request, you will get back all features in this bounding box from the states layer, and the only values of each feature returned will be the STATE_NAME and PERSONS attributes. Along with those, you will also get back the feature ID.

Head over to the demo page and try it out. Use the same url as before (http://localhost:8080/geoserver/wfs) and use the request above.

Insert Feature Transaction

The Insert Feature request is wrapped in a Transaction to ensure that no one else is editing the data at the same time and so that the change only takes effect if everything succeedes.

This request will insert a new road into the Tasmania_roads dataset.
Notice the only two types of data in the road dataset: geometry (gml:the_geom) and type (topp:TYPE).

<wfs:Transaction service="WFS" version="1.0.0"
  xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:topp="http://www.openplans.org/topp"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd http://www.openplans.org/topp http://localhost:8080/geoserver/wfs/DescribeFeatureType?typename=topp:tasmania_roads">
  <wfs:Insert>
    <topp:tasmania_roads>
      <topp:the_geom>
        <gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#27354">
          <gml:lineStringMember>
            <gml:LineString>
              <gml:coordinates decimal="." cs="," ts=" ">
494475.71056415,5433016.8189323 494982.70115662,5435041.95096618
              </gml:coordinates>
            </gml:LineString>
          </gml:lineStringMember>
        </gml:MultiLineString>
      </topp:the_geom>
      <topp:TYPE>mytest</topp:TYPE>
    </topp:tasmania_roads>
  </wfs:Insert>
</wfs:Transaction>

Head over to the demo page and try it out. Use the same url as before (http://localhost:8080/geoserver/wfs) and use the request above.

Delete Feature Transaction

Another request is the Delete Feature request. This one is also performed within a transaction.

The request looks like this:

<wfs:Transaction service="WFS" version="1.0.0"
  xmlns:cdf="http://www.opengis.net/cite/data"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:wfs="http://www.opengis.net/wfs">
  xmlns:topp="http://www.openplans.org/topp">
  <wfs:Delete typeName="topp:tasmania_roads">
    <ogc:Filter>
      <ogc:PropertyIsEqualTo>
        <ogc:PropertyName>topp:TYPE</ogc:PropertyName>
        <ogc:Literal>mytest</ogc:Literal>
      </ogc:PropertyIsEqualTo>
    </ogc:Filter>
  </wfs:Delete>
</wfs:Transaction>

It will delete all features that are in the filter. Here we are deleting the road we just inserted, the one with the type mytest.

Head over to the demo page and try it out. Use the same url as before (http://localhost:8080/geoserver/wfs) and use the request above.

Extra

Have extra time?
Play around on the Demo Request page and try the other WFS requests, such as Update Feature and Describe Feature Type

Document generated by Confluence on Jan 16, 2008 23:27